package com.hhl.flower.controller;

import com.hhl.flower.common.constant.JwtClaimsConstant;
import com.hhl.flower.common.properties.JwtProperties;
import com.hhl.flower.common.result.Result;
import com.hhl.flower.common.utils.JwtUtil;
import com.hhl.flower.context.BaseContext;
import com.hhl.flower.pojo.dto.UserLoginDTO;
import com.hhl.flower.pojo.dto.UserRegisterDTO;
import com.hhl.flower.pojo.entity.User;
import com.hhl.flower.pojo.vo.UserLoginVO;
import com.hhl.flower.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user/user")
@Api(tags = "用户相关接口")
@Slf4j
public class UserController {
	@Autowired
	private UserService userService;
	@Autowired
	private JwtProperties jwtProperties;


	@PostMapping("/login")
	@ApiOperation("用户登录")
	public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
		log.info("用户登录：{}", userLoginDTO);

		User user = userService.login(userLoginDTO);

		//登录成功后，生成jwt令牌
		Map<String, Object> claims = new HashMap<>();
		claims.put(JwtClaimsConstant.ID, user.getId());
		String token = JwtUtil.createJWT(
				jwtProperties.getUserSecretKey(),
				jwtProperties.getUserTtl(),
				claims);

		UserLoginVO userLoginVO = UserLoginVO.builder()
				.tokenHead("Authorization")
				.token(token)
				.build();

		return Result.success(userLoginVO);
	}

	@PostMapping("/register")
	@ApiOperation(value = "用户注册")
	public Result register(@RequestBody UserRegisterDTO userRegisterDTO) {
		log.info("用户注册：{}", userRegisterDTO);

		userService.register(userRegisterDTO);

		return Result.success();
	}

	@GetMapping("/personal/info")
	@ApiOperation(value = "查询当前用户信息")
	public Result<User> getPersonal() {
		Integer uId = BaseContext.getCurrentId();

		User user = userService.getById(uId);

		return Result.success(user);
	}

	@PostMapping("/update/info")
	@ApiOperation(value = "用户信息更新")
	public Result update(@RequestParam("nickname") String nickname, @RequestParam("gender") Integer gender, @RequestParam("phone") String phone, @RequestParam("email") String email, @RequestParam("avatar") String avatar, @RequestParam("description") String description) {
		Integer currentId = BaseContext.getCurrentId();
		User user = userService.getById(currentId);
		user.setNickname(nickname);
		user.setGender(gender);
		user.setPhone(phone);
		user.setEmail(email);
		user.setAvatar(avatar);
		user.setDescription(description);

		userService.updateUserInfo(user);

		return Result.success();
	}

	@PostMapping("/upload/avatar")
	@ApiOperation(value = "上传用户头像")
	public Result<String> updateUserAvatar(@RequestParam("file") MultipartFile file) throws IOException {
		Integer uId = BaseContext.getCurrentId();

		String url = userService.updateUserAvatar(uId, file);

		return Result.success(url);
	}

}
